package com.mes.web.controller.biz;

import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.BizCost;
import com.mes.biz.domain.BizCostDetail;
import com.mes.biz.domain.vo.BizCostVo;
import com.mes.biz.service.BizCostDetailService;
import com.mes.biz.service.BizCostService;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.R;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收支单Controller
 * @author wy
 * @email wy
 * @date 2023-08-25
 */
@RestController
@RequestMapping("/biz/bizCost")
public class BizCostController extends BaseController {

    @Autowired
    private BizCostService bizCostService;
    @Autowired
    private BizCostDetailService bizCostDetailService;

    /**
     * 查询收支单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCost:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizCost bizCost, HttpServletRequest request, HttpServletResponse response) {
        bizCost.setPage(new PageDomain(request, response));
        return R.data(bizCostService.findPage(bizCost));
    }

    /**
     * 获取收支单详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCost:query')")
    @GetMapping(value = "/{id}")
    public R<BizCost> detail(@PathVariable("id") String id) {
        BizCost bizCost = bizCostService.get(id);
        List<BizCostDetail> costDetailList = bizCostDetailService.selectByCostId(id);
        bizCost.setBizCostDetailList(costDetailList);
        return R.data(bizCost);
    }

    /**
     * 新增收支单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCost:add')")
    @Log(title = "收支单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated BizCost bizCost) {
        return R.status(bizCostService.saveCost(bizCost));
    }

    /**
     * 修改收支单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCost:edit')")
    @Log(title = "收支单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizCost bizCost) {
        return R.status(bizCostService.saveCost(bizCost));
    }

    /**
     * 删除收支单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCost:remove')")
    @Log(title = "收支单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(bizCostService.deleteBizCostByIds(ids));
    }


    /**
     * 导出收支单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCost:export')")
    @Log(title = "收支单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(BizCost bizCost) {
        List<BizCost> list = bizCostService.findList(bizCost);
        ExcelUtil<BizCost> util = new ExcelUtil<BizCost>(BizCost.class);
        return util.exportExcel(list, "收支单数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    @Log(title = "审核收支单", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/audit/{id}")
    public R auditPurchase(@PathVariable String id) {
        return R.data(bizCostService.auditCost(id));
    }

    /**
     * 查询收支统计列表
     */
    @GetMapping("/listStat")
    public R<PageInfo> listStat(BizCost bizCost, HttpServletRequest request, HttpServletResponse response) {
        bizCost.setPage(new PageDomain(request, response));
        PageInfo<BizCostVo> page = bizCostService.listStatPage(bizCost);
        return R.data(page);
    }

    /**
     * 打印收支单
     */
    @GetMapping("/printStat")
    public void export(BizCost bizCost, HttpServletResponse response) {
        List<BizCostVo> list = bizCostService.listStat(bizCost);
        ExcelUtil<BizCostVo> util = new ExcelUtil<>(BizCostVo.class);
        util.printExcel(response,list, "收支统计表");
    }

}
